//
//  HRCoverTimeModel.m
//  Hear
//
//  Created by mac on 2017/4/14.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRCoverTimeModel.h"

@implementation HRCoverTimeModel
+(NSString *)covetTime:(NSString *)covetTime{
    if(!covetTime.length) return @"";
    NSMutableString *mutableStringWithT = [[NSMutableString alloc] initWithString:covetTime];
    
    
    if ([mutableStringWithT rangeOfString:@"T"].location != NSNotFound){
        [mutableStringWithT replaceCharactersInRange:[mutableStringWithT rangeOfString:@"T"] withString:@" "];
        NSArray *array = [mutableStringWithT componentsSeparatedByString:@"."];
        covetTime = [array firstObject];
    }
    
   
    
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSDate *date = [[NSDate alloc] init];
    // voila!
    date = [dateFormatter dateFromString:covetTime];
    
    
    
    NSTimeInterval coveTimeIn = [date timeIntervalSince1970];
    
    NSDate *coveDate = [NSDate dateWithTimeIntervalInMilliSecondSince1970:(NSTimeInterval)coveTimeIn];
    
    
    return [coveDate formattedTime];//
}

+(NSString *)covetMyDynamicTime:(NSString *)covetTime{
    if(!covetTime.length) return @"";
    NSMutableString *mutableStringWithT = [[NSMutableString alloc] initWithString:covetTime];
    
    
    if ([mutableStringWithT rangeOfString:@"T"].location != NSNotFound){
        [mutableStringWithT replaceCharactersInRange:[mutableStringWithT rangeOfString:@"T"] withString:@" "];
        NSArray *array = [mutableStringWithT componentsSeparatedByString:@"."];
        covetTime = [array firstObject];
    }
    
    
    
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    // voila!
    NSDate *date = [dateFormatter dateFromString:covetTime];
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:date];
    NSInteger day = [components day];
    NSInteger month= [components month];
    NSInteger year= [components year];

    return [NSString stringWithFormat:@"%d.%d.%d",year,month,day];//
}

+(NSString *)covetActiveTime:(NSString *)str// 三分钟前

{
    if(!str.length) return @"";
    NSMutableString *mutableStringWithT = [[NSMutableString alloc] initWithString:str];
    
    
    if ([mutableStringWithT rangeOfString:@"T"].location != NSNotFound){
        [mutableStringWithT replaceCharactersInRange:[mutableStringWithT rangeOfString:@"T"] withString:@" "];
        NSArray *array = [mutableStringWithT componentsSeparatedByString:@"."];
        str = [array firstObject];
    }
    //把字符串转为NSdate
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSDate *timeDate = [dateFormatter dateFromString:str];
    
    NSDate *currentDate = [NSDate date];
    
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:timeDate];
    
    long temp = 0;
    
    NSString *result;
    
    if (timeInterval/60 < 1) {
        
        result = [NSString stringWithFormat:@"刚刚"];
        
    }
    
    else if((temp = timeInterval/60) <60){
        
        result = [NSString stringWithFormat:@"%ld分钟前",temp];
        
    }
    
    else if((temp = temp/60) <24){
        
        result = [NSString stringWithFormat:@"%ld小时前",temp];
        
    }
    
    else if((temp = temp/24) <30){
        
        result = [NSString stringWithFormat:@"%ld天前",temp];
        
    }
    
    else if((temp = temp/30) <12){
        
        result = [NSString stringWithFormat:@"%ld月前",temp];
        
    }
    
    else{
        
        temp = temp/12;
        
        result = [NSString stringWithFormat:@"%ld年前",temp];
        
    }
    
    return  result;
    
}

+(NSString *)covetMyDynamicBottomTime:(NSString *)covetTime{
    if(!covetTime.length) return @"";
    NSMutableString *mutableStringWithT = [[NSMutableString alloc] initWithString:covetTime];
    
    
    if ([mutableStringWithT rangeOfString:@"T"].location != NSNotFound){
        [mutableStringWithT replaceCharactersInRange:[mutableStringWithT rangeOfString:@"T"] withString:@" "];
        NSArray *array = [mutableStringWithT componentsSeparatedByString:@"."];
        covetTime = [array firstObject];
    }
    
    
    
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSDate *date = [[NSDate alloc] init];
    // voila!
    date = [dateFormatter dateFromString:covetTime];
    
    
    
    NSTimeInterval coveTimeIn = [date timeIntervalSince1970];
    
    NSDate *coveDate = [NSDate dateWithTimeIntervalInMilliSecondSince1970:(NSTimeInterval)coveTimeIn];
    
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"HH:mm"];
    
    
    NSDateFormatter* formatterH = [[NSDateFormatter alloc]init];
    [formatterH setDateFormat:@"HH"];
    if ([[formatterH stringFromDate:coveDate] intValue ] >12){
        return [NSString stringWithFormat:@"PM %@",[formatter stringFromDate:coveDate]];
    }else{
        return [NSString stringWithFormat:@"AM %@",[formatter stringFromDate:coveDate]];
    }
}



//计算两个日期之间的天数
+ (NSInteger) calcDaysFromBegin:(NSString *)beginTime end:(NSDate *)endTime
{
    if (!beginTime.length) return 100000;
    NSMutableString *mutableStringWithT = [[NSMutableString alloc] initWithString:beginTime];
    
    
    if ([mutableStringWithT rangeOfString:@"T"].location != NSNotFound){
        [mutableStringWithT replaceCharactersInRange:[mutableStringWithT rangeOfString:@"T"] withString:@" "];
        NSArray *array = [mutableStringWithT componentsSeparatedByString:@"."];
        beginTime = [array firstObject];
    }
    
    NSDate *now = [NSDate date];
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *oldDate = [dateFormatter dateFromString:beginTime];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    unsigned int unitFlags = NSDayCalendarUnit;
    NSDateComponents *comps = [gregorian components:unitFlags fromDate:oldDate  toDate:now  options:0];
    
    NSInteger days= [comps day];
    //int hours=((int)time)%(3600*24)/3600;
    //NSString *dateContent=[[NSString alloc] initWithFormat:@"%i天%i小时",days,hours];
    return days;
}


@end
